package com.leslie.hhcommunity.controller;

import com.leslie.hhcommunity.entity.Event;
import com.leslie.hhcommunity.entity.Page;
import com.leslie.hhcommunity.entity.User;
import com.leslie.hhcommunity.event.EventProducer;
import com.leslie.hhcommunity.service.FollowService;
import com.leslie.hhcommunity.service.UserService;
import com.leslie.hhcommunity.util.CommunityConstant;
import com.leslie.hhcommunity.util.CommunityUtil;
import com.leslie.hhcommunity.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author
 * @PackageName:com.leslie.hhcommunity.controller
 * @ClassnName:FollowController
 * @Description:
 * @Date 2022/1/6  12:29
 */
@Controller
public class FollowController implements CommunityConstant {

	@Autowired
	FollowService followService;
	@Autowired
	HostHolder hostHolder;
	@Autowired
	UserService userService;
	@Autowired
	EventProducer eventProducer;

	/**
	 * 关注
	 *
	 * @param entityType
	 * @param entityId 用户的id
	 * @return
	 */
	@RequestMapping(value = "/follow", method = RequestMethod.POST)
	@ResponseBody
	public String follow(int entityType, int entityId) {
		User user = hostHolder.getUser();
		followService.follow(user.getId(), entityType, entityId);

		// 触发关注事件
		Event event = new Event()
				.setTopic(TOPIC_FOLLOW)
				.setUserId(hostHolder.getUser().getId())
				.setEntityType(entityType)
				.setEntityId(entityId)
				.setEntityUserId(entityId);
		eventProducer.fireEvent(event);

		return CommunityUtil.getJSONString(0, "已关注");
	}

	/**
	 * 取关
	 *
	 * @param entityType
	 * @param entityId
	 * @return
	 */
	@RequestMapping(value = "/unfollow", method = RequestMethod.POST)
	@ResponseBody
	public String unfollow(int entityType, int entityId) {
		User user = hostHolder.getUser();
		followService.unfollow(user.getId(), entityType, entityId);
		return CommunityUtil.getJSONString(0, "已取消关注");
	}

	/**
	 * 关注的人的列表
	 * （他关注了）
	 *
	 * @param userId
	 * @param page
	 * @param model
	 * @return
	 */
	@RequestMapping("/followees/{userId}")
	public String getFollowees(@PathVariable("userId") int userId, Page page, Model model) {
		User user = userService.findUserById(userId);
		if (user == null) {
			throw new RuntimeException("该用户不存在");
		}
		model.addAttribute("user", user);
		page.setLimit(5);
		page.setPath("/followees/" + userId);
		page.setRows((int) followService.findFolloweeCount(userId, ENTITY_TYPE_USER));

		List<Map<String, Object>> userLists = followService.findFollowees(userId, page.getOffset(), page.getLimit());
		if (userLists != null) {
			for (Map<String, Object> map : userLists) {
				User u = (User) map.get("user");
				map.put("hasFollowed", hasFollowed(u.getId()));
			}
		}
		model.addAttribute("users", userLists);
		return "site/followee";
	}

	/**
	 * 粉丝列表
	 * （他的粉丝）
	 *
	 * @param userId
	 * @param page
	 * @param model
	 * @return
	 */
	@RequestMapping("/followers/{userId}")
	public String getFollowers(@PathVariable("userId") int userId, Page page, Model model) {
		User user = userService.findUserById(userId);
		if (user == null) {
			throw new RuntimeException("该用户不存在");
		}
		model.addAttribute("user", user);
		page.setLimit(5);
		page.setPath("/followers/" + userId);
		page.setRows((int) followService.findFollowerCount(ENTITY_TYPE_USER, userId));

		List<Map<String, Object>> userLists = followService.findFollowers(userId, page.getOffset(), page.getLimit());
		if (userLists != null) {
			for (Map<String, Object> map : userLists) {
				User u = (User) map.get("user");
				map.put("hasFollowed", hasFollowed(u.getId()));
			}
		}
		model.addAttribute("users", userLists);
		return "site/follower";
	}


	/**
	 * 当前用户是否关注id为userId的用户
	 *
	 * @param userId
	 * @return
	 */
	private boolean hasFollowed(int userId) {
		if (hostHolder.getUser() == null) {
			return false;
		}
		return followService.hasFollowed(hostHolder.getUser().getId(), ENTITY_TYPE_USER, userId);
	}

}
